<!DOCTYPE html>
<head>

    <title>进货单</title>

    @include('web.layout.head')
    @include('web.layout.header_proscenium')
    <link rel="stylesheet" href="{{asset('/css/web/order/cart.css')}}">
    <link rel="stylesheet" href="{{asset('/js/web/frame/layer-v3.0.3/layer/skin/default/layer.css')}}">
</head>
<body>
<div class="shopcar-header">
    <div class="w1200 f-cb">
        <a class="logo fl" href="{{asset('/')}}"><img src="{{asset('/img/web/logo/forget_logo.png')}}" alt=""></a>
        <div class="tit fl">进货单</div>
        <img class="flow fr" src="{{asset('/img/web/order/img114.jpg')}}" alt="" class="fr">
        <img class="flow1 fr" src="{{asset('/img/web/order/img1141.jpg')}}" alt="" class="fr">
    </div>
</div>
<div class="shopcar-none w-f" style="display: none;">
    <div class="center f-cb">
        <span class="fl ico"></span>
        <div class="fr cont">
            <h3>您的进货单还是空的，赶快去挑选商品吧！</h3>
            <p>·　去看看新发布的<a href="">最新产品</a></p>
            <p>·　去看看正在折扣中的优品<a href=" ">促销</a></p>
        </div>
    </div>
</div>
<div class="w1200 shopcar-wrap" style="min-height: 600px;">
    <span class="pro-all proo pro-normal">
{{--        <a data-id="1" href="{{env('OLD_APP_URL').'/order/cart'}}"> 自营商品</a>--}}
        <a data-id="3" id="ch" class="dq" onclick="show_table('allgoods','no_allgoods','ch',1)"> 撮合商品&nbsp;&nbsp;<span class="num-case"></span></a>
        <a  data-id="2" id="sx"  onclick="show_table('no_allgoods','allgoods','sx',2)"> 失效&nbsp;&nbsp;{{isset($invalid_count)?$invalid_count:0}}</a>
    </span>
    <form action="/member/buyer/orders/create" method="get" accept-charset="utf-8" class="form-horizontal" id="frm-post-login" name="frm-post">
        {{csrf_field()}}
        <input type="hidden" name="type" value="cart">
        <div class="car-tab allgoodsxz" id="allgoods">
            <table width="100%">
                <thead>
                <tr>
                    <th width="70">
                        <label class="product_normal lab fl">
                            <input class="fl check-all" type="checkbox" name="check-all">
                            <span class="fl">全选</span>
                        </label>
                    </th>
                    <th class="tex-ct" width="510">商品信息</th>
                    <!-- <th>商品信息</th> -->
                    <th class="tex-ct" width="200">单价(元)</th>
                    <th class="tex-ct" width="100">数量</th>
                    <th class="tex-ct" width="253">小计(元)</th>
                    <th width="107">操作</th>
                </tr>
                </thead>
                @if(isset($valid_shops) && !empty($valid_shops))
                    @foreach($valid_shops as $key=>$shop)
                        <tr class="product_normal store{{$shop['sp_id']}}">
                            <td class="erjiallxz01" colspan="6">
                                    <input type="hidden" checked="checked" class="check00">
                                <input class="fl check-store" checked="checked" type="checkbox" data-storenum="{{$shop['sp_id']}}">
                                <a href="/shop/index/{{$shop['sp_id']}}" target="_blank"><span>{{$shop['sp_name']}}</span></a>
                            </td>
                        </tr>
                        @if($shop['carts'])
                            @foreach($shop['carts'] as $cart)
                                <tr class="pro-li product_normal product_normal_tr">
                                    <td style="width: 50px" class="storenum{{$shop['sp_id']}}"><input data-storenum="{{$shop['sp_id']}}" key="{{$cart['sku']['id']}}" company-id="{{$shop['sp_id']}}" value="1" name="product_data[{{$cart['sku_id']}}][check]" cart-id="{{$cart['id']}}" class="check check01" type="checkbox"></td>
                                    <td>
                                        <a class="img fl" href="/product/info/{{$cart['product']['id']}}/{{$cart['sku']['id']}}" target="_blank">
                                            <img style="width:70px;height:70px;" src="{{$cart['product']['cover_image']}}" alt="">
                                        </a>
                                        <div class="fl cont">
                                            <a href="/product/info/{{$cart['product']['id']}}/{{$cart['sku']['id']}}" class="name" target="_blank">{{$cart['product']['name']}}</a>
                                            <span>{{$cart['product']['model']}}</span>
                                        </div>
                                    </td>
                                    <td class="unit tex-ct" style="color:#cc0000">￥<span class="unit">{{$cart['sku']['sale_price'] ? : 0.00}}</span></td>
                                    <td style="width:150px;">
                                        <span class="fl cut_type cut butn">-</span>
                                        <input name="product_data[{{$cart['sku_id']}}][num]" sum-sum="{{$cart['sku']['stock']}}" class="fl num_type num  num_key_{{$shop['sp_id']}}_{{$cart['sku']['id']}}" type="text" value="{{$cart['num'] > $cart['sku']['stock'] ? $cart['sku']['stock'] : $cart['num']}}">
                                        <span class="fl add_type add butn">+</span>
                                        @if($cart['num'] > $cart['sku']['stock'])
                                            <span style="width: 100%;display: inline-block;text-align: center;">库存仅剩{{$cart['sku']['stock']}}件</span>
                                        @endif
                                    </td>
                                    <td class="tex-ct sum-price">
                                        <input name="product_data[{{$cart['sku_id']}}][sku_id]" style="display:none;" type="text" value="{{$cart['sku_id']}}">
                                        <span></span>
                                        <div class="freight" data-freight="">￥<em class="sum-pri sum_key_{{$shop['sp_id']}}_{{$cart['sku']['id']}}">
                                                <?php $price_total=$cart['sku']['sale_price'] * ($cart['num'] > $cart['sku']['stock'] ? $cart['sku']['stock'] : $cart['num']);?>
                                                {{sprintf("%.2f",$price_total)}}</em>
                                        </div>
                                    </td>
                                    <td>
                                        <span class="dele" cat_id="{{$cart['id']}}">删除</span>
                                    </td>
                                </tr>
                                <?php
                                    if($cart['num'] > $cart['sku']['stock']){
                                        $understock = true;
                                    }
                                ?>
                            @endforeach
                        @endif
                    @endforeach
                @else
                    <tr class="product_normal">
                        <td colspan='6' style='text-align: center;'>没有数据</td>
                    </tr>
                @endif

                @if (count($errors) > 0)
                      <tr class="">
                          @foreach ($errors->all() as $error)
                              <td colspan='6' class="erjiallxz01" style='text-align: center; color: #cc0000;'>{{ $error }}</td>
                         @endforeach
                     </tr>
                 @endif
            </table>

        </div>
        <div class="car-tab allgoodsxz" id="no_allgoods" style="display: none;">
            <table width="100%">
                <thead>
                <tr>
                    <th width="70">

                    </th>
                    <th class="tex-ct" width="510">商品信息</th>
                    <th class="tex-ct" width="200">单价(元)</th>
                    <th class="tex-ct" width="100"></th>
                    <th class="tex-ct" width="253"></th>
                    <th width="107">操作</th>
                </tr>
                </thead>
                @if(isset($invalid_shops) && !empty($invalid_shops))
                    @foreach($invalid_shops as $key=>$shop)
                        <tr class="product_normal store1">
                            <td class="erjiallxz02" colspan="6">
                                <input type="hidden" checked="checked" class="check00">
                                <span>{{$shop['sp_name']}}</span>
                            </td>
                        </tr>
                        @if($shop['carts'])
                            @foreach($shop['carts'] as $cart)
                                <tr class="pro-li product_normal ">
                                    <td style="width: 50px" class="storenum1">
                                        <input type="hidden" class="check02" data-id="{{$cart['id']}}">
                                    </td>
                                    <td>
                                        <a class="img fl" href="/product/info/{{$cart['product']['id']}}/{{$cart['sku']['id']}}" target="_blank">
                                            <img style="width:70px;height:70px;" src="{{$cart['product']['cover_image']}}" alt="">
                                        </a>
                                        <div class="fl cont">
                                            <a href="/product/info/{{$cart['product']['id']}}/{{$cart['sku']['id']}}" class="name" target="_blank">{{$cart['product']['name']}}</a>
                                            <span>{{$cart['product']['model']}}</span>
                                        </div>
                                    </td>
                                    <td class="unit tex-ct" style="color:#cc0000">￥<span class="unit">{{$cart['sku']['sale_price']}}</span></td>
                                    <td style="width:150px;">
                                    </td>
                                    <td class="tex-ct sum-price">
                                    </td>
                                    <td>
                                        <span class="dele" cat_id="{{$cart['id']}}">删除</span>
                                    </td>
                                </tr>
                            @endforeach
                        @endif
                    @endforeach
                @else
                    <tr class="product_normal">
                        <td colspan='6' style='text-align: center;'>没有数据</td>
                    </tr>
                @endif
            </table>

        </div>

        <div class="bot-operate f-cb" id="bot_show">
            <label class="product_normal lab fl">
                <input class="check fl" type="checkbox">
                <span class="fl">全选</span>
            </label>
            <span class="fl dele-all product_normal" id='delete_audit'>删除</span>
            <!-- <span class="fr sub-btn">去付款</span> -->
            <!--正常 可以付款的class换成sub-btn-->
            @if(isset($valid_shops) && !empty($valid_shops))
                <input type="submit" class="fr sub-btn product_normal" id="sub-btn" value="去结算">
            @else
                <input type="button" class="fr sub-btn product_normal" id="sub-btn" value="去结算">
            @endif


            <span class="fr price product_normal">￥ <span
                        id="all-pri"></span></span>
            <span class="fr font product_normal">共有 <i class="num-case"></i> 件商品 <span>合计（不含运费）</span>　
            </span>
        </div>
        <div class="bot-operate f-cb" id="bot_hide" style="display: none;">
            <span id='delete_no_audit' class="fl  dele-all product_lose">清空失效商品</span>
        </div>

    </form>
</div>
@include('web.layout.footer_ware1')
</body>
<script type="text/javascript">
    var csrf_token = '{{csrf_token()}}'
    $(document).ready(function () {
        jQuery.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': csrf_token
            },
            error: function(jqXHR, textStatus, errorThrown){
                if(jqXHR.status >= 500){
                    var msg = '服务器错误';
                }else if(jqXHR.status >= 400){
                    var msg = '路由错误或无权限';
                }else{
                    var msg = '未知错误';
                }

                layer.alert(msg, {
                    icon: 1
                }, function () {
                    location.reload();
                });
            }
        });
    });

    @if(isset($understock) && $understock)
        layer.alert('部分商品库存不足！', {
            icon: 0
        });
    @endif
    <?php if(isset($cart_data)){?>
    layer.alert('您当前还未登录，请前去登录！', {
        icon: 0
    }, function () {
        location.href = '/user/login';
    });
    <?php }?>

    var carts_delete_url = '/member/buyer/carts/0';


    function show_table(id,id2,id3,id4){
        $('#'+id).css('display','block');
        $('#'+id2).css('display','none');
        $('#'+id3).addClass('dq');
        $('#'+id3).siblings().removeClass('dq');
        if(id4==1){
            $('#bot_show').css('display','block');
            $('#bot_hide').css('display','none');
        }else{
            $('#bot_show').css('display','none');
            $('#bot_hide').css('display','block');
        }
    }
    $(document).ready(function () {
//临时注释，不使用选项卡，使用跳转(到aoce.91)。
//        $(".pro-normal a").on('click', function (event) {
//            event.preventDefault();
//            /* Act on the event */
//            $(".pro-normal a").removeClass('dq');
//            $(this).addClass('dq');
//            var id = $(this).attr('data-id');
//            if(id == 2){
//                $("#no_allgoods").show();
//                $("#allgoods").hide();
//                $("#bot_hide").show();
//                $("#bot_show").hide();
//            }else{
//                $("#allgoods").show();
//                $("#no_allgoods").hide();
//                $("#bot_show").show();
//                $("#bot_hide").hide();
//            }
//        });





        $(".car-tab .add_type").click(function () {
            var sum_num = $(this).parent().find("input").attr('sum-sum');
            var n = parseInt($(this).parent().find("input").val());//购物个数
            var id = parseInt($(this).parents("tr").data('id'));//购物id
            if (sum_num - 1 < n) {
                layer.alert('库品不足', {icon: 1});
                n = sum_num - 1;
            }
            $(this).parent().find("input").val(n + 1);
            var unit = parseFloat($(this).parents('tr').find("span.unit").text());
            //var sum_pri = parseFloat($(this).parents('tr').find("em.sum-pri").text());
            $(this).parents('tr').find("em.sum-pri").text(( (n + 1) * unit).toFixed(2));
            var all_pri = parseFloat($('#all-pri').text());
            $('#all-pri').text((all_pri + unit).toFixed(2));
            var num = parseFloat($(".num-case").text());
            $(".num-case").text(num + 1);
            _charges();
            // cartChange(id, n + 1);//个数
            // refreshMoney();
        })
        $(".car-tab .cut_type").click(function () {
            var sum_num = $(this).parent().find("input").attr('sum-sum');
            var n = parseInt($(this).parent().find("input").val());
            var id = parseInt($(this).parents("tr").data('id'));//购物id
            if (n > 1) {
                if (sum_num < n) {
                    layer.alert('库品不足', {icon: 1});
                    n = sum_num;
                }
                $(this).parent().find("input").val(n - 1);
                var unit = parseFloat($(this).parents('tr').find("span.unit").text());
                //var sum_pri = parseFloat($(this).parents('tr').find("em.sum-pri").text());
                $(this).parents('tr').find("em.sum-pri").text((unit * (n - 1)).toFixed(2));
                var all_pri = parseFloat($('#all-pri').text());
                $('#all-pri').text((all_pri - unit).toFixed(2));
                var num = parseFloat($(".num-case").text());
                $(".num-case").text(num - 1);
                _charges();
                // cartChange(id, n - 1);//个数
                // refreshMoney();
            } else {
                $(this).parent().find("input").val(1);
                //$(this).parents('tr').find("em.sum-pri").text((unit).toFixed(2));
                _charges();
                // cartChange(id, 1);//个数
                // refreshMoney();
            }
        })
        $(".car-tab .num_type").keyup(function () {
            var sum_num = $(this).parent().find("input").attr('sum-sum');
            var n = parseInt($(this).parent().find("input").val());
            var id = parseInt($(this).parents("tr").data('id'));//购物id
            if (n >= 1) {
                if (sum_num < n) {
                    layer.alert('库品不足', {icon: 1});
                    n = sum_num;
                }
                $(this).parent().find("input").val(n);
                var unit = parseFloat($(this).parents('tr').find("span.unit").text());
                $(this).parents('tr').find("em.sum-pri").text((unit * n).toFixed(2));
                _charges();
            } else {
                $(this).parent().find("input").val(1);
                $(this).parents('tr').find("em.sum-pri").text((unit).toFixed(2));
                _charges();
            }
        });

        $(".car-tab tr .check01").click(function () {
            judge = _charges();

            if (1 == judge) {
                $('#sub-btn').removeClass('sub-btn-no');
                $('#sub-btn').attr('type','submit');
                $('#sub-btn').addClass('sub-btn');
                $('#sub-btn').show();
            } else {
                $('#sub-btn').removeClass('sub-btn');
                $('#sub-btn').addClass('sub-btn-no');
                $('#sub-btn').attr('type','button');
                $('#sub-btn').show();
            }
            // refreshMoney();
            var storenum = $(this).attr('data-storenum');
            if($(".storenum"+storenum).find("input[type='checkbox']").length != $(".storenum"+storenum).find("input[type='checkbox']:checked").length){
                $(".store"+storenum).find(".check-store")[0].checked = false;
            }else{
                $(".store"+storenum).find(".check-store")[0].checked = true;
            }
            if($(".product_normal_tr").find("input[type='checkbox']").length != $(".product_normal_tr").find("input[type='checkbox']:checked").length){
                 $('input[name="check-all"]').prop("checked",false);
                 $(".bot-operate").find("input")[0].checked = false;
            }else{
                 $('input[name="check-all"]').prop("checked",true);
                 $(".bot-operate").find("input")[0].checked = true;
            }
        })
        $(".check-store").click(function () {
            var storenum = $(this).attr('data-storenum');
            var isSelect = this.checked;
            if (isSelect) {
                $(".storenum"+storenum).each(function () {
                    $(this).find("input")[0].checked = true;
                });
            } else {
                $(".storenum"+storenum).each(function () {
                    $(this).find("input")[0].checked = false;
                });
            }
            if($(".product_normal_tr").find("input[type='checkbox']").length != $(".product_normal_tr").find("input[type='checkbox']:checked").length){
                 $('input[name="check-all"]').prop("checked",false);
                 $(".bot-operate").find("input")[0].checked = false;
            }else{
                 $('input[name="check-all"]').prop("checked",true);
                 $(".bot-operate").find("input")[0].checked = true;
            }
            _charges();
        })
        $(".check-lose").click(function (event) {
            /* Act on the event */
            $(".check-all-lose").click();
        });
        $(".check-all-lose").click(function () {
            var isSelect = this.checked;
            if (isSelect) {
                $(".product_lose_tr").each(function () {
                    $(this).find("input")[0].checked = true;
                });
                $(".bot-operate").find('.check-lose')[0].checked = true;
            } else {
                $(".product_lose_tr").each(function () {
                    $(this).find("input")[0].checked = false;
                });
                $(".bot-operate").find('.check-lose')[0].checked = false;
                // refreshMoney();
            }
        });
        $(".car-tab .check-all").click(function () {
            var isSelect = this.checked;
            if ($(this).is(":checked")) {
                $('#sub-btn').removeClass('sub-btn-no');
                $('#sub-btn').addClass('sub-btn');
                $('#sub-btn').attr('type','submit');

                $('#sub-btn').show();
            } else {
                $('#sub-btn').removeClass('sub-btn');
                $('#sub-btn').addClass('sub-btn-no');
                $('#sub-btn').attr('type','button');
                $('#sub-btn').show();
            }

            if (isSelect) {
                $(".product_normal_tr").each(function () {
                    $(this).find("input")[0].checked = true;
                });
                $(".bot-operate").find('.check')[0].checked = true;
                // refreshMoney();
                $(".erjiallxz01").each(function () {
                    $(this).find(".check-store")[0].checked = true;
                });
            } else {
                $(".product_normal_tr").each(function () {
                    $(this).find("input")[0].checked = false;
                });
                $(".bot-operate").find('.check')[0].checked = false;
                // refreshMoney();
                $(".erjiallxz01").each(function () {
                    $(this).find(".check-store")[0].checked = false;
                });
            }
            _charges();
        })
        $(".bot-operate .check").click(function () {
            if ($(this).is(":checked")) {
                $('#sub-btn').removeClass('sub-btn-no');
                $('#sub-btn').addClass('sub-btn');
                $('#sub-btn').show();
            } else {
                $('#sub-btn').removeClass('sub-btn');
                $('#sub-btn').addClass('sub-btn-no');
                $('#sub-btn').show();
            }
            $(".car-tab .check-all").click();
        });

        $(".dele").on('click', function (event) {
            event.preventDefault();
            /* Act on the event */
            var cart_ids = Array($(this).attr('cat_id'));
            layer.confirm('是否删除', {
                btn: ['确认', '取消'] //可以无限个按钮
            }, function (index) {
                $.ajax({
                    url: carts_delete_url,
                    type: 'DELETE',
                    dataType: 'json',
                    data: {'type': 'batch','cart_ids': cart_ids},
                    success: function (data) {
                        if (0 == data.code) {
                            layer.alert(data.message, {
                                icon: 1
                            }, function () {
                                location.reload();
                            });
                        } else {
                            layer.alert(data.message, {
                                icon: 1
                            });
                        }
                    }
                });
            }, function (index, layero) {
                layer.close(index);
            });
        });

        $("#delete_no_audit").on('click', function (event) {
            event.preventDefault();
            /* Act on the event */
            layer.confirm('是否删除', {
                btn: ['确认', '取消'] //可以无限个按钮
            }, function (index) {
                var cart_ids = new Array();
                $(".check02").each(function (index, el) {
                    cart_ids.push($(this).data('id'));
                });
                $.ajax({
                    url: carts_delete_url,
                    type: 'DELETE',
                    dataType: 'json',
                    data: {'type' : 'batch', 'cart_ids': cart_ids},
                    success: function (data) {
                        if (0 == data.code) {
                            layer.alert(data.message, {
                                icon: 1
                            }, function () {
                                location.reload();
                            });
                        } else {
                            layer.alert(data.message, {
                                icon: 1
                            });
                        }
                    }
                });
            }, function (index, layero) {
                layer.close(index);
            });
        });

        $("#delete_audit").on('click', function (event) {
            event.preventDefault();
            /* Act on the event */
            layer.confirm('是否删除', {
                btn: ['确认', '取消'] //可以无限个按钮
            }, function (index) {
                var cart_ids = new Array();
                $(".check01").each(function (index, el) {
                    if ($(this).is(":checked")) {
                        cart_ids.push($(this).attr('cart-id'));
                    }
                });
                $.ajax({
                    url: carts_delete_url,
                    type: 'DELETE',
                    dataType: 'json',
                    data: {'type' : 'batch', 'cart_ids': cart_ids},
                    success: function (data) {
                        if (0 == data.code) {
                            layer.alert(data.message, {
                                icon: 1
                            }, function () {
                                location.reload();
                            });
                        } else {
                            layer.alert(data.message, {
                                icon: 1
                            });
                        }
                    }
                });

            }, function (index, layero) {
                layer.close(index);
            });
        });

        $('.check-all').trigger("click");

        function _charges() {
            var judge = 0;
            var sum_money = 0;
            var sum_n = 0;
            $(".product_normal .check01").each(function (index, el) {
                if ($(this).is(":checked")) {
                    judge = 1;
                    var key = $(this).attr('key');
                    var company_id = $(this).attr('company-id');
                    var n = parseInt($(".num_key_" + company_id + "_" + key).val());
                    sum_n = sum_n + n;
                    var money = parseFloat($(".sum_key_" + company_id + "_" + key).text());
                    sum_money = sum_money + money;
                }
            });
            $(".num-case").text(sum_n);
            if (0 == sum_money) {
                $('#all-pri').text(sum_money);
            } else {
                $('#all-pri').text(sum_money.toFixed(2));
            }
            return judge;
        }

    })
</script>
</html>